package com.hanbang.taitaigou.ui.activity.base;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hanbang.taitaigou.R;
import com.hanbang.taitaigou.model.UserInforData;
import com.hanbang.taitaigou.ui.activity.login.LogInActivity;
import com.hanbang.taitaigou.utils.db.DbUtil;
import com.hanbang.taitaigou.utils.other.ObjectUtils;
import com.hanbang.taitaigou.utils.other.SharedPreferencesUtils;
import com.hanbang.taitaigou.utils.other.StringUtils;
import com.hanbang.taitaigou.utils.ui.LogUtils;
import com.hanbang.taitaigou.utils.ui.SystemBarTintManager;
import com.hanbang.taitaigou.utils.ui.ToastUtils;
import com.hanbang.taitaigou.utils.ui.UiUtils;

import org.xutils.DbManager;
import org.xutils.HttpManager;
import org.xutils.ImageManager;
import org.xutils.ex.DbException;
import org.xutils.x;

public class BaseActivity extends FragmentActivity {
    protected String TAG = BaseActivity.class.getSimpleName();
    public static UserInforData userData = null;
    public HttpManager httpManager = x.http();
    public DbManager dbManager = DbUtil.getDbUtils();
    public ImageManager imageManager = x.image();
    public int statusBarHeight = 0;
    public boolean isTopHint = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTranslucentStatus(isTopHint);
        getDbUserData();


    }

    public void setTranslucentStatus(boolean on) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //状态栏透明 需要在创建SystemBarTintManager 之前调用。
            Window win = getWindow();
            WindowManager
                    .LayoutParams winParams = win.getAttributes();
            final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            if (on) {
                winParams.flags |= bits;
            } else {
                winParams.flags &= ~bits;
            }
            win.setAttributes(winParams);
            SystemBarTintManager mTintManager = new SystemBarTintManager(this);
            mTintManager.setStatusBarTintEnabled(true);
            mTintManager.setNavigationBarTintEnabled(true);
            mTintManager.setTintResource(R.color.main_color);
            statusBarHeight = mTintManager.getConfig().getStatusBarHeight();
        }
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        x.view().inject(this);
        UiUtils.applyFont(UiUtils.getRootView(this));
        if (isTopHint) {
            UiUtils.getRootView(this).setPadding(0, statusBarHeight - 2, 0, 0);
        }

    }

    public void getDbUserData() {
        int id = SharedPreferencesUtils.getSharedPreferencesKeyAndValue(this,
                SharedPreferencesUtils.USER_DATA, "user_id", -1);
        if (id != -1) {
            try {
                userData = dbManager.findById(UserInforData.class, id);
            } catch (DbException e) {
                e.printStackTrace();
                userData = null;
            }
        } else {
            userData = null;
        }
    }

    public void setDbUserData() {
        if (userData != null) {
            try {
                dbManager.saveOrUpdate(userData);
            } catch (DbException e) {
                e.printStackTrace();
            }
        }
    }

    public void setDbUserData(UserInforData userD) {
        userData = userD;
    }


    public boolean isLogin(boolean isToLogin) {
        if (userData == null) {
            if (isToLogin) {
                Intent intent = new Intent(this, LogInActivity.class);
                startActivity(intent);
            }
            LogUtils.e(TAG, "未登录");
            return false;
        } else {
            LogUtils.e(TAG, "已登录");
            return true;
        }
    }

    public void passwordIsError(boolean isToast) {
        SharedPreferencesUtils.setSharedPreferencesKeyAndValue(BaseActivity.this,
                SharedPreferencesUtils.USER_DATA, "user_id", -1);
        userData = null;
        if (isToast) {
            ToastUtils.show(this, "已退出,请重新登录!", Toast.LENGTH_SHORT);
            /**
             * 发送退出成功的广播
             */
            Intent intent = new Intent();
            intent.setAction("tuichu");
            sendBroadcast(intent);
            return;
        }
        /**
         * 发送登录成功的广播
         */
        Intent intent = new Intent();
        intent.setAction(LogInActivity.FINISH_FLAG);
        sendBroadcast(intent);
    }


    /*
     * 设置回退键
     */
    public void setBackArrows(View back) {
        if (back instanceof ViewGroup) {
            ViewGroup b = (ViewGroup) back;
            b.setVisibility(View.VISIBLE);
            b.getChildAt(1).setVisibility(View.GONE);
            ViewGroup.LayoutParams params = b.getChildAt(0).getLayoutParams();
            params.width = ObjectUtils.dip2px(this, 15);
            params.height = ObjectUtils.dip2px(this, 15);
            UiUtils.setButtonClickTint(back, R.color.white, R.color.gray_f8);
            back.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        } else if (back instanceof ImageView) {
            back.setVisibility(View.VISIBLE);
            back.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();

    }

    /**
     * 必须是  bar_top_arr_title_text  布局
     * <p>
     * 字符串为空就不显示
     * 资源为小于0就不显示图片
     *
     * @param left     左边文字
     * @param leftRes  左边图片资源
     * @param title
     * @param right
     * @param rightRes
     */
    public void setTop(String left, int leftRes, String title, String right, int rightRes) {
        TextView leftTv = (TextView) findViewById(R.id.top_bar_left_tv);
        ImageView leftIv = (ImageView) findViewById(R.id.top_bar_left_iv);

        TextView titleTv = (TextView) findViewById(R.id.top_bar_title);

        TextView rightTv = (TextView) findViewById(R.id.top_bar_right_tv);
        ImageView rightIv = (ImageView) findViewById(R.id.top_bar_right_iv);
        setTopOneView(leftTv, left);
        setTopOneView(leftIv, leftRes);
        setTopOneView(titleTv, title);
        setTopOneView(rightTv, right);
        setTopOneView(rightIv, rightRes);
    }

    private static void setTopOneView(View view, Object text) {
        if (view instanceof TextView) {
            TextView tv = (TextView) view;
            if (tv != null) {
                String tx = (String) text;
                if (StringUtils.isBlank(tx)) {
                    tv.setVisibility(View.GONE);
                } else {
                    tv.setVisibility(View.VISIBLE);
                    tv.setText(tx);
                }
            }
        } else if (view instanceof ImageView) {
            ImageView iv = (ImageView) view;
            if (iv != null) {
                int tx = (int) text;
                if (tx <= 0) {
                    iv.setVisibility(View.GONE);
                } else {
                    iv.setVisibility(View.VISIBLE);
                    iv.setImageResource(tx);
                }
            }
        }

    }
}
